// pages/wallet/wallet.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    kpinfo:true,
    kpinfobott:false,
    array: ['充值','订单'],
    index: 0, //类型选择
    date: '', //时间
    endTime: '', //结束时间,
    timesum:'', //时间切换
    invoices:[],
    flag: false,
    seletItem: {},
    openlist:0,   //可开票
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let  kpinfo = (options.kpinfo == 'false') ? false : true
    let  kpinfobott = (options.kpinfobott == 'true') ? true : false
    let that = this;
    let time = that.current();
    let timesum = that.data.timesum;
    that.setData({
      date: time,
      kpinfo,
      kpinfobott,
      openlist:options.openlist
    })


    let time2 = that.before();
    that.setData({
      endTime: time2,
      timesum:time2
    })

    that.onquery();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  navigateBack(){
    wx.switchTab({
      url: '/pages/me/me',
    })
  },
//
  onquery(e){
    let that = this;
    let openlist = that.data.openlist;
    let token = wx.getStorageSync('token')
    let date = that.data.date;
    let endTime = that.data.endTime;
    let index = that.data.index;
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx?cmd=FindBills',
      data: {
       //请求后台的分页数据
       cmd:'FindBills',
       Token:token,
       Fapiao:openlist,
       DateFrom:date ,
       DateTo:endTime,
       FeeType:index
      },
      method: "get",
      success(res) {
        console.log(res);
       var data = res.data.DATA;
       that.setData({
        invoices:data,
       })
      
      }
   })

  },

  onReady() {

  },

  /**切换开票**/
  onkpinfo(e){
    let that = this;
    let kpinfo = that.data.kpinfo;
    let kpinfobott = that.data.kpinfobott;
    // let openlist = that.data.openlist;
    that.setData({
      kpinfo:true,
      kpinfobott:false,
      openlist:0
    })
    that.onquery();
  },
  onkpinfoout(){
    let that = this;
    let kpinfo = that.data.kpinfo;
    let kpinfobott = that.data.kpinfobott;
    that.setData({
      kpinfo:false,
      kpinfobott:true,
      openlist:1
    })
    that.onquery();
  },


  // 时间切换
  bindDateChange(e) {
    let that = this;
    console.log(e);
    this.setData({
      date: e.detail.value,
      timesum:e.detail.value
    })
    that.onquery();
  },
  // 类型切换
  bindPickerChange(e) {
    let that = this;
    //改变index值，通过setData()方法重绘界面
    that.setData({
      index: e.detail.value
    });
    that.onquery();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 获取当前时间
  current() {
    // 时间戳
    let time = +new Date();
    let date = new Date(time),
      Y = date.getFullYear(), // 年  
      M = date.getMonth(), //月  
      D = date.getDate(), //日
      h = date.getHours(), //小时
      m = date.getMinutes(), //分钟
      s = date.getSeconds(); //秒数
    let timer = Y + '-' + this.getZero(M) + '-' + this.getZero(D) + ' '
    return timer;

  },
  //以前时间戳
  before(){
    // 时间戳
    let time = +new Date();
    let date = new Date(time),
      Y = date.getFullYear(), // 年  
      M = date.getMonth()+1, //月  
      D = date.getDate(), //日
      h = date.getHours(), //小时
      m = date.getMinutes(), //分钟
      s = date.getSeconds(); //秒数
    let timer = Y + '-' + this.getZero(M) + '-' + this.getZero(D) + ' '
    return timer;
  },
  getZero(num) { // 个位数前补0
    if (parseInt(num) < 10) {
      num = '0' + num;
    }
    return num;
  },


  // 数据选择
  selectData(e) {
    // 当前选择的是那条
    let item = e.currentTarget.dataset.item;
    let flag = e.currentTarget.dataset.flag;
    let seletItem = this.data.seletItem;
    // 数据
    let data = this.data.invoices;
    console.log(data);
    data.forEach((outItem) => {
      if (outItem.OrderNo === item.OrderNo) {
        if(flag==true){
          outItem.flag = false;
        }else{
          //将点击的数据变成红色
          outItem.flag = true;
          // 将选择了的数据保存起来。跳转之后携带item
          this.setData({
            seletItem: outItem
          })
      } 

      } else {
        // 如果当前点击和其他的不同，其他的都变成false
        outItem.flag = false;
      }
    })

    // 数据变化
    this.setData({
      invoices: data,
    })
  },
  jumpToBilling() {
    let that = this;
    // 判断当前对象是否为空
    let arr = that.data.seletItem;
    let bid = arr.BID;
    let fee = arr.Fee;
    console.log(arr);
console.log(8888888)
    if (arr.flag == true) {
      //跳转到发票管理
      wx.navigateTo({
        url: '../../pages/confirmBilling/confirmBilling?BID=' + bid + '&Fee=' +fee,
      })
    }else{
      wx.showToast({
        title: '请点击选择',
        icon: 'error',
        duration: 2000
      })
    }

  }



})